home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / fntool / readfnt.c < prev    next >
Text File  |  1993-12-06  |  2KB  |  74 lines

  1. #include "fntool.h"
  2. #include "../include/grx.h"
  3. #include "../include/grxfile.h"
  4.  
  5. static void badfnt(void)
  6. {
  7.     fatalerr("invalid \".fnt\" file: \"%s\"",inname);
  8. }
  9.  
  10. static void readdata(void *buffer,int size)
  11. {
  12.     if(read(fileno(infile),buffer,size) != size) badfnt();
  13. }
  14.  
  15. void readfnt(void)
  16. {
  17.     FntFileHdr hdr;
  18.     short *wtable = NULL;
  19.     char  *bitmap,*bp;
  20.     chr   *cp,*last;
  21.     int   ii,xx,yy,mask,byte,width;
  22.  
  23.     readdata(&hdr,sizeof(FntFileHdr));
  24.     if(hdr.magic != FONT_MAGIC) badfnt();
  25.     if(!hdr.h.fnt_isfixed) {
  26.         ii = (hdr.h.fnt_maxchar - hdr.h.fnt_minchar + 1) * sizeof(short);
  27.         readdata((wtable = safemalloc(ii)),ii);
  28.     }
  29.     bitmap = safemalloc(hdr.bitmapsize);
  30.     readdata(bitmap,hdr.bitmapsize);
  31.     for(bp = notes; (ii = getc(infile)) != EOF; *bp++ = ii);
  32.     *bp = '\0';
  33.     fnt.height  = hdr.h.fnt_height;
  34.     fnt.minchar = hdr.h.fnt_minchar;
  35.     fnt.maxchar = hdr.h.fnt_maxchar;
  36.     fnt.isfixed = hdr.h.fnt_isfixed;
  37.     strcpy(fnt.name,hdr.h.fnt_name);
  38.     strcpy(fnt.family,hdr.h.fnt_family);
  39.     last = NULL;
  40.     bp = bitmap;
  41.     for(ii = fnt.minchar; ii <= fnt.maxchar; ii++) {
  42.         width = fnt.isfixed ? hdr.h.fnt_width : wtable[ii - fnt.minchar];
  43.         cp = safemalloc(sizeof(chr));
  44.         cp->bmp = makebytemap(width,fnt.height);
  45.         for(yy = 0; yy < fnt.height; yy++) {
  46.         byte = mask = 0;
  47.         for(xx = 0; xx < width; xx++) {
  48.             if((mask >>= 1) == 0) { byte = *bp++; mask = 0x80; }
  49.             cp->bmp[yy][xx] = (byte & mask) ? 1 : 0;
  50.         }
  51.         }
  52.         cp->code  = ii;
  53.         cp->width = width;
  54.         *(last ? &last->next : &fnt.chars) = cp;
  55.         last = cp;
  56.     }
  57.     free(bitmap);
  58.     if(!hdr.h.fnt_isfixed) free(wtable);
  59.     computewidth();
  60.     splitfamily();
  61.     if((fnt.undwidth = hdr.h.fnt_undwidth) == 0) {
  62.         fnt.undwidth = (fnt.height / 20) + 1;
  63.     }
  64.     if((fnt.baseline = hdr.h.fnt_baseline) == 0) {
  65.         fnt.baseline = fnt.height - 1;
  66.         if((cp = getchr('a')) != NULL) {
  67.         while((fnt.baseline > 0) && !rowbit(cp->bmp,fnt.baseline)) {
  68.             fnt.baseline--;
  69.         }
  70.         }
  71.     }
  72. }
  73.  
  74.